/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.entityTable;
import sql.AConnection;



/**
 *
 * @author aragon
 */
public class User extends AConnection implements entityTable<User>{
    String idUser;
    String name;
    String userName;
    String password;
    String dvct;
    String iddvpt;
    String tendvpt;
    String address;
    String phone;
    String job;
    String email;
    String date_register;
    String lever;
    String birthday;
    String sex;
    String active;

    public User(String idUser, String name, String userName, String password, String dvct, String iddvpt,String tendvpt, String address, String phone, String job, String email, String date_register, String lever, String birthday, String sex, String active) {
        this.idUser = idUser;
        this.name = name;
        this.userName = userName;
        this.password = password;
        this.dvct = dvct;
        this.iddvpt = iddvpt;
        this.tendvpt =tendvpt;
        this.address = address;
        this.phone = phone;
        this.job = job;
        this.email = email;
        this.date_register = date_register;
        this.lever = lever;
        this.birthday = birthday;
        this.sex = sex;
        this.active = active;
    }

    public User() {
        this.idUser = "";
        this.name = "";
        this.userName = "";
        this.password = "";
        this.dvct = "";
        this.iddvpt = "";
        this.tendvpt ="";
        this.address = "";
        this.phone = "";
        this.job = "";
        this.email = "";
        this.date_register = "";
        this.lever = "";
        this.birthday = "";
        this.sex = "1";
        this.active = "";
    }

     public boolean  select() {
        try {
            String query = "SELECT HoTen,Username ,dvct,iddvpt,DiaChi,Dienthoai,"
                    + "nghenghiep,Email,NgayDangKy,idGroup,NgaySinh,GioiTinh,Active FROM"
                    + " users where idUser=?  limit 0,1";
            String query1="SELECT iddvpt,TenDV FROM donviphutrach where iddvpt=? and AnHien=1";
            Connection connection=getConnection();
            PreparedStatement ps=connection.prepareStatement(query);
            ps.setString(1, idUser);
            ResultSet rs = ps.executeQuery();
            
            
            if (rs.next()) {
                PreparedStatement ps1= connection.prepareStatement(query1);
                 ps1.setString(1, rs.getString(4));
                 ResultSet rs1 = ps1.executeQuery();
                 if(rs1.next()){
                     setIddvpt(rs1.getString(1));
                setTendvpt(rs1.getString(2));
                 }else{
                     setIddvpt("-1");
                     setTendvpt("Chưa Xác Định");
                 }
                setName(rs.getString(1));
                setUserName(rs.getString(2));
                setDvct(rs.getString(3));
                
                setAddress(rs.getString(5));
                setPhone(rs.getString(6));
                setJob(rs.getString(7));
                setEmail(rs.getString(8));
                setDate_register(rs.getString(9));
                setLever(rs.getString(10));
                setBirthday(rs.getString(11));
                setSex(rs.getString(12));
                setActive(rs.getString(13));
                freeConnection(connection);
                return true;
            } else {
                freeConnection(connection);
                return false;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean insert() {
        try {


            String query = "INSERT INTO quanlyphonghoc.users (idUser, HoTen, Username"
                    + ", Password, dvct, iddvpt, DiaChi, Dienthoai, nghenghiep, Email,"
                    + " NgayDangKy, idGroup, NgaySinh, GioiTinh, Active) VALUES (NULL,"
                    + "?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            Connection connection=getConnection();
            PreparedStatement ps=connection.prepareStatement(query);
            ps.setString(1,getName());
            ps.setString(2, getUserName());
            ps.setString(3, getPassword());
            ps.setString(4, getDvct());
            ps.setString(5, getIddvpt());
            ps.setString(6,getAddress());
            ps.setString(7, getPhone());
            ps.setString(8, getJob());
            ps.setString(9, getEmail());
            ps.setString(10, getDate_register());
            ps.setString(11, getLever());
            ps.setString(12, getBirthday());
            ps.setString(13, getSex());
            ps.setString(14, getActive());
            int re = ps.executeUpdate();
            freeConnection(connection);
            return true;

        } catch (SQLException ex) {

            ex.printStackTrace();
            return false;
        }
    }



    public boolean update() {
        try{
        String query ="UPDATE users SET HoTen = ?, dvct = ?,"
                + " iddvpt = ?, DiaChi = ?, Dienthoai = ? ,"
                + " nghenghiep = ?, Email = ?, NgaySinh = ?, GioiTinh = ?,idGroup=?,Active=?"
                + " WHERE idUser = ? LIMIT 1";
       Connection connection=getConnection();
       PreparedStatement ps=connection.prepareStatement(query);
       ps.setString(1,getName() );
       ps.setString(2,getDvct() );
       ps.setString(3,getIddvpt() );
       ps.setString(4,getAddress() );
       ps.setString(5,getPhone() );
       ps.setString(6,getJob() );
       ps.setString(7,getEmail() );
       ps.setString(8,getBirthday() );
       ps.setString(9,getSex());
       ps.setString(10,getLever());
       ps.setString(11,getActive());
       ps.setString(12, getIdUser());
       boolean re=ps.execute();
       freeConnection(connection);

       
       return !re;
        }catch(Exception e){
            System.out.println(e.toString());
            return false;
        }

    }

    public boolean remove() {
        try {
            String query = "DELETE FROM users where idUser=?";
            Connection connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setString(1, idUser);
            ps.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;

        }
    }

   

    public boolean  checkLogin() {
        
        try {
            String query = "SELECT idUser,HoTen,Username ,Password,idGroup FROM"
                    + " users where Username=? and Active=1 limit 0,1";
            Connection connection=getConnection();
            PreparedStatement statement = connection.prepareStatement(query);
            statement.setString(1, getUserName());
            ResultSet rs= statement.executeQuery();
            
            
            if (rs.next() &&rs.getString("Password").equals(getPassword())) {
                setIdUser(rs.getString("idUser"));
                setName(rs.getString("HoTen"));
                setLever(rs.getString("idGroup"));
                freeConnection(connection);
                return true;
            } else{
                freeConnection(connection);
                return false;
            }

        } catch (Exception ex) {
           
           return false;
        }

    }
    

    

    public ResultSet selectDB() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getDate_register() {
        return date_register;
    }

    public void setDate_register(String date_register) {
        this.date_register = date_register;
    }

    public String getDvct() {
        return dvct;
    }

    public void setDvct(String dvct) {
        this.dvct = dvct;
    }

    public String getIddvpt() {
        return iddvpt;
    }

    public void setIddvpt(String iddvpt) {
        this.iddvpt = iddvpt;
    }

    public String getTendvpt() {
        return tendvpt;
    }

    public void setTendvpt(String tendvpt) {
        this.tendvpt = tendvpt;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getIdUser() {
        return idUser;
    }

    public void setIdUser(String idUser) {
        this.idUser = idUser;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getLever() {
        return lever;
    }

    public void setLever(String lever) {
        this.lever = lever;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }









}
